package cn.app.android.sichuanxiaoke.com.xiaokeapp;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
import java.util.List;
import cn.app.android.sichuanxiaoke.com.xiaokeapp.fragment.ClassifyFragment;
import cn.app.android.sichuanxiaoke.com.xiaokeapp.fragment.CommunicationFragment;
import cn.app.android.sichuanxiaoke.com.xiaokeapp.fragment.HomeFragment;
import cn.app.android.sichuanxiaoke.com.xiaokeapp.fragment.IntelligenFragment;
import cn.app.android.sichuanxiaoke.com.xiaokeapp.fragment.MineFragment;

public class AppHomeActivity extends AppCompatActivity implements View.OnClickListener {

    //    private ViewPager mMyViewPager;
    private List<Fragment> fragments;
    //    private MyViewPagerAdapter adapter;
    private ImageView mWebPageHomeImg,mClassifyImg,mManUserImg,mCommunicationImg,mIntelligenImg;
    private TextView mWebPageHomeTv,mClassifyTv,mManUserTv,mCommunicationtv,mIntelligentv;
    private LinearLayout mWebPageHomeLl,mClassifyLl,mManUserLl,mCommunication,mIntelligen;
    private FragmentManager fm;
    private Fragment cF,mF,hF,tF,iF;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //getSupportActionBar().hide();
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        mWebPageHomeImg = (ImageView) findViewById(R.id.web_page_home_img);
        mWebPageHomeTv = (TextView) findViewById(R.id.web_page_home_tv);
        mClassifyImg = (ImageView) findViewById(R.id.classify_img);
        mClassifyTv = (TextView) findViewById(R.id.classify_tv);
        mManUserImg = (ImageView) findViewById(R.id.man_user_img);
        mManUserTv = (TextView) findViewById(R.id.man_user_tv);
        mCommunicationImg= (ImageView) findViewById(R.id.communication_img);
        mCommunicationtv = (TextView) findViewById(R.id.communication_tv);
        mIntelligenImg = (ImageView) findViewById(R.id.intelligen_img);
        mIntelligentv = (TextView) findViewById(R.id.intelligen_tv);
        setTheFirst();
//        mMyViewPager = (ViewPager) findViewById(R.id.my_view_pager);
        initFragment();
//        adapter = new MyViewPagerAdapter(getSupportFragmentManager(), fragments);
//        mMyViewPager.setAdapter(adapter);
//        mMyViewPager.setOffscreenPageLimit(3);
//        mMyViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
//            @Override
//            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//
//            }
//
//            @Override
//            public void onPageSelected(int position) {
//                switch (position) {
//                    case 0:
//                        showmWebPageHomeLl();
//                        break;
//                    case 1:
//                        showmClassifyLl();
//                        break;
//                    case 2:
//                        showmManUserLl();
//                        break;
//                }
//            }
//
//            @Override
//            public void onPageScrollStateChanged(int state) {
//
//            }
//        });
        mWebPageHomeLl = (LinearLayout) findViewById(R.id.web_page_home_ll);
        mClassifyLl = (LinearLayout) findViewById(R.id.classify_ll);
        mManUserLl = (LinearLayout) findViewById(R.id.man_user_ll);
        mCommunication= (LinearLayout) findViewById(R.id.communication);
        mIntelligen= (LinearLayout) findViewById(R.id.intelligen);
        mClassifyLl.setOnClickListener(this);
        mWebPageHomeLl.setOnClickListener(this);
        mManUserLl.setOnClickListener(this);
        mCommunication.setOnClickListener(this);
        mIntelligen.setOnClickListener(this);
        fm = getSupportFragmentManager();
        hF = new HomeFragment();
        cF = new ClassifyFragment();
        mF = new MineFragment();
        tF = new CommunicationFragment();
        iF = new IntelligenFragment();
        fm.beginTransaction().add(R.id.dl_container,cF).commit();
        fm.beginTransaction().add(R.id.dl_container,mF).commit();
        fm.beginTransaction().add(R.id.dl_container,hF).commit();
        fm.beginTransaction().add(R.id.dl_container,tF).commit();
        fm.beginTransaction().add(R.id.dl_container,iF).commit();
        fm.beginTransaction().show(hF).hide(cF).hide(mF).hide(iF).hide(tF).commit();
    }

    private void setTheFirst() {
        showmWebPageHomeLl();
    }

    private void initFragment() {
        fragments = new ArrayList<>();
        fragments.add(new HomeFragment());
        fragments.add(new ClassifyFragment());
        fragments.add(new MineFragment());
    }

    @Override
    public void onClick(View view) {
        if (view == mClassifyLl){
//            mMyViewPager.setCurrentItem(1);
            fm.beginTransaction().show(cF).hide(tF).hide(hF).hide(iF).hide(mF).commit();
            showmClassifyLl();
        }else if (view == mManUserLl){
//            mMyViewPager.setCurrentItem(2);
            fm.beginTransaction().show(mF).hide(cF).hide(hF).hide(tF).hide(iF).commit();
            showmManUserLl();
        }else if (view == mWebPageHomeLl){
//            mMyViewPager.setCurrentItem(0);
            fm.beginTransaction().show(hF).hide(cF).hide(mF).hide(iF).hide(tF).commit();
            showmWebPageHomeLl();
        }else if (view == mCommunication){
//            mMyViewPager.setCurrentItem(0);
            fm.beginTransaction().show(tF).hide(cF).hide(mF).hide(iF).hide(hF).commit();
            showmComunication();
        }else if (view == mIntelligen){
//            mMyViewPager.setCurrentItem(0);
            fm.beginTransaction().show(iF).hide(cF).hide(mF).hide(hF).hide(tF).commit();
            showmIntelligen();
        }

    }

    private void showmWebPageHomeLl() {
        Glide.with(getBaseContext()).load(R.mipmap.web_page_home_red).into(mWebPageHomeImg);
        mWebPageHomeTv.setTextColor(getResources().getColor(R.color.red));
        Glide.with(getBaseContext()).load(R.mipmap.classify).into(mClassifyImg);
        mClassifyTv.setTextColor(getResources().getColor(R.color.darkgray));
        Glide.with(getBaseContext()).load(R.mipmap.man_user).into(mManUserImg);
        mManUserTv.setTextColor(getResources().getColor(R.color.darkgray));
        Glide.with(getBaseContext()).load(R.mipmap.communication).into(mCommunicationImg);
        mCommunicationtv.setTextColor(getResources().getColor(R.color.darkgray));
        Glide.with(getBaseContext()).load(R.mipmap.intelligen).into(mIntelligenImg);
        mIntelligentv.setTextColor(getResources().getColor(R.color.darkgray));

    }

    private void showmManUserLl() {
        Glide.with(getBaseContext()).load(R.mipmap.web_page_home).into(mWebPageHomeImg);
        mWebPageHomeTv.setTextColor(getResources().getColor(R.color.darkgray));
        Glide.with(getBaseContext()).load(R.mipmap.classify).into(mClassifyImg);
        mClassifyTv.setTextColor(getResources().getColor(R.color.darkgray));
        Glide.with(getBaseContext()).load(R.mipmap.man_user_red).into(mManUserImg);
        mManUserTv.setTextColor(getResources().getColor(R.color.red));
        Glide.with(getBaseContext()).load(R.mipmap.communication).into(mCommunicationImg);
        mCommunicationtv.setTextColor(getResources().getColor(R.color.darkgray));
        Glide.with(getBaseContext()).load(R.mipmap.intelligen).into(mIntelligenImg);
        mIntelligentv.setTextColor(getResources().getColor(R.color.darkgray));
    }

    private void showmClassifyLl() {
        Glide.with(getBaseContext()).load(R.mipmap.web_page_home).into(mWebPageHomeImg);
        mWebPageHomeTv.setTextColor(getResources().getColor(R.color.darkgray));
        Glide.with(getBaseContext()).load(R.mipmap.classify_red).into(mClassifyImg);
        mClassifyTv.setTextColor(getResources().getColor(R.color.red));
        Glide.with(getBaseContext()).load(R.mipmap.man_user).into(mManUserImg);
        mManUserTv.setTextColor(getResources().getColor(R.color.darkgray));
        Glide.with(getBaseContext()).load(R.mipmap.communication).into(mCommunicationImg);
        mCommunicationtv.setTextColor(getResources().getColor(R.color.darkgray));
        Glide.with(getBaseContext()).load(R.mipmap.intelligen).into(mIntelligenImg);
        mIntelligentv.setTextColor(getResources().getColor(R.color.darkgray));
    }
    private void showmComunication() {
        Glide.with(getBaseContext()).load(R.mipmap.web_page_home).into(mWebPageHomeImg);
        mWebPageHomeTv.setTextColor(getResources().getColor(R.color.darkgray));
        Glide.with(getBaseContext()).load(R.mipmap.classify).into(mClassifyImg);
        mClassifyTv.setTextColor(getResources().getColor(R.color.darkgray));
        Glide.with(getBaseContext()).load(R.mipmap.man_user).into(mManUserImg);
        mManUserTv.setTextColor(getResources().getColor(R.color.darkgray));
        Glide.with(getBaseContext()).load(R.mipmap.communication_red).into(mCommunicationImg);
        mCommunicationtv.setTextColor(getResources().getColor(R.color.red));
        Glide.with(getBaseContext()).load(R.mipmap.intelligen).into(mIntelligenImg);
        mIntelligentv.setTextColor(getResources().getColor(R.color.darkgray));
    }
    private void showmIntelligen() {
        Glide.with(getBaseContext()).load(R.mipmap.web_page_home).into(mWebPageHomeImg);
        mWebPageHomeTv.setTextColor(getResources().getColor(R.color.darkgray));
        Glide.with(getBaseContext()).load(R.mipmap.classify).into(mClassifyImg);
        mClassifyTv.setTextColor(getResources().getColor(R.color.darkgray));
        Glide.with(getBaseContext()).load(R.mipmap.man_user).into(mManUserImg);
        mManUserTv.setTextColor(getResources().getColor(R.color.darkgray));
        Glide.with(getBaseContext()).load(R.mipmap.communication).into(mCommunicationImg);
        mCommunicationtv.setTextColor(getResources().getColor(R.color.darkgray));
        Glide.with(getBaseContext()).load(R.mipmap.intelligen_red).into(mIntelligenImg);
        mIntelligentv.setTextColor(getResources().getColor(R.color.red));
    }

}
